package com.crane.app.web.controller;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crane.common.core.dto.Response;
import com.crane.common.core.dto.SingleResponse;
import com.crane.common.core.utils.BaseContextHandler;
import com.crane.oss.app.bean.req.AppQueryTaskRopeDayDetailReq;
import com.crane.oss.app.bean.resp.AppStudentInfo;
import com.crane.oss.app.bean.resp.AppStudentSkipRopeResp;
import com.crane.oss.app.bean.resp.TaskStudentSkipPopeResp;
import com.crane.oss.school.bean.request.equipment.AddEquipmentReq;
import com.crane.oss.school.bean.request.studentequipment.UpdateStudentEquipmentReq;
import com.crane.oss.school.bean.request.task.AddTaskRopeReq;
import com.crane.oss.school.bean.request.task.EndTaskRopeReq;
import com.crane.oss.school.bean.request.task.QueryHistoryDataReq;
import com.crane.oss.school.bean.request.task.QueryRankReq;
import com.crane.oss.school.bean.request.task.QueryStudentTaskRopeListReq;
import com.crane.oss.school.bean.resp.HistorySkipRopeResp;
import com.crane.oss.school.bean.resp.RankSkipRopeResp;
import com.crane.oss.school.bean.resp.StudentEquipmentIndexResp;
import com.crane.oss.school.bean.resp.StudentResp;
import com.crane.oss.school.bean.resp.TaskStudentSkipRopeResp;
import com.crane.oss.school.bean.resp.TodaySkipRopeResp;
import com.crane.oss.school.service.EquipmentService;
import com.crane.oss.school.service.StudentEquipmentService;
import com.crane.oss.school.service.StudentService;
import com.crane.oss.school.service.StudentSkipropeService;
import com.crane.oss.school.service.TaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Api(value = "AppStudentSkipRopeController", tags = {"任务-跳绳（学生）"})
@RestController
@RequestMapping("/app/student/skipRope")
public class AppStudentSkipRopeController extends BaseAppController {

    @Resource
    TaskService taskService;

    @Resource
    StudentEquipmentService studentEquipmentService;

    @Resource
    StudentSkipropeService studentSkipropeService;

    @Resource
    EquipmentService equipmentService;

    @Resource
    StudentService studentService;


    @ApiOperation(value = "学生跳绳任务列表", notes = "首页-学生跳绳任务列表")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryStudentTaskRopeListReq")
    @PostMapping("/queryTaskRopeList")
    public SingleResponse<IPage<AppStudentSkipRopeResp>> queryTaskRopeStudentList(@RequestBody QueryStudentTaskRopeListReq request) {
        Long userId= BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        request.setStudentId(studentResp.getStudentId());
        return SingleResponse.of(taskService.queryTaskRopeStudentList(request));
    }

    @ApiOperation(value = "首页-我的跳绳", notes = "首页-我的跳绳")
    @GetMapping("/mySkipRope")
    public SingleResponse<List<StudentEquipmentIndexResp>> queryStudentEquipmentList() {
        Long userId= BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        return SingleResponse.of(studentEquipmentService.queryEquipmentForIndex(studentResp.getStudentId()));
    }

    @ApiOperation(value = "今日跳绳与跳绳练习", notes = "首页-今日跳绳与跳绳练习")
    @GetMapping("/skipRopeInfo")
    public SingleResponse<TodaySkipRopeResp> queryTodaySkipRope() {
        Long userId= BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        return SingleResponse.of(studentSkipropeService.queryTodaySkipRope(studentResp.getStudentId()));
    }

    @ApiOperation(value = "历史数据", notes = "历史数据")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryHistoryDataReq")
    @PostMapping("/history")
    public SingleResponse<HistorySkipRopeResp> queryTodaySkipRope(@RequestBody QueryHistoryDataReq request){
        Long userId= BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        return SingleResponse.of(studentSkipropeService.queryHistoryData(request));
    }

    @ApiOperation(value = "排行榜", notes = "排行榜")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryRankReq")
    @PostMapping("/rank")
    public SingleResponse<RankSkipRopeResp> queryRank(@RequestBody QueryRankReq request){
        Long userId= BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        return SingleResponse.of(studentSkipropeService.queryRank(request));
    }

    @ApiOperation(value = "查询任务详情", notes = "查询任务详情")
    @ApiImplicitParam(name = "taskId", value = "任务id", paramType = "path", dataType = "Long")
    @GetMapping("/detail/{taskId}")
    public SingleResponse<TaskStudentSkipPopeResp> queryStudentSkipRopeDetail(@PathVariable("taskId") Long taskId) {
        Long userId= BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        return SingleResponse.of(taskService.queryStudentSkipRopeDetail(taskId, studentResp.getStudentId()));
    }

    @ApiOperation(value = "查询任务当日打卡详情", notes = "查询任务当日打卡详情")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AppQueryTaskRopeDayDetailReq")
    @PostMapping("/queryStudentSkipRopeDayDetail")
    public SingleResponse<TaskStudentSkipRopeResp> queryStudentSkipRopeDayDetail(@RequestBody AppQueryTaskRopeDayDetailReq request) {
        Long userId= BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        request.setStudentId(studentResp.getStudentId());
        return taskService.queryStudentSkipRopeDayDetail(request);
    }

    @ApiOperation(value = "开始跳绳", notes = "开始跳绳")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddTaskRopeReq")
    @PostMapping("/addTaskRopeReq")
    public SingleResponse<Long> addTaskRopeReq(@RequestBody AddTaskRopeReq request) {
        Long userId= BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        request.setStudentId(studentResp.getStudentId());
        return taskService.addTaskRopeReq(request);
    }


    @ApiOperation(value = "结束跳绳", notes = "结束跳绳")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "EndTaskRopeReq")
    @PostMapping("/endTaskRopeReq")
    public SingleResponse<Boolean> endTaskRopeReq(@RequestBody EndTaskRopeReq request) {
        Long userId= BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        request.setStudentId(studentResp.getStudentId());
        return taskService.endTaskRopeReq(request);
    }

    @ApiOperation(value = "新增跳绳设备", notes = "新增跳绳设备并与学生绑定")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddEquipmentReq")
    @PostMapping("/addEquipment")
    public Response addEquipment(@RequestBody AddEquipmentReq request) {
    	equipmentService.save(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改跳绳设备", notes = "修改：设备名称、绑定状态、使用状态")
    @ApiImplicitParam(name = "request", value = "请求对象",required = true, dataType = "UpdateStudentEquipmentReq")
    @PostMapping("/updateEquipment")
    public Response updateEquipment(@RequestBody UpdateStudentEquipmentReq request) {
    	studentEquipmentService.update(request);
    	return Response.buildSuccess();
    }

    @ApiOperation(value = "首页-设置-学生列表", notes = "首页-设置-学生列表")
    @ApiImplicitParam(name = "appUserId", value = "用户ID", paramType = "path", dataType = "appUserId")
    @GetMapping("/studentInfo/{appUserId}")
    public SingleResponse<List<AppStudentInfo>> appQueryStudentInfo(@PathVariable("appUserId") Long appUserId) {
    	return SingleResponse.of(studentService.appQueryStudentInfo(appUserId));
    }



    @ApiOperation(value = "获取设备模式", notes = "获取设备模式")
    @GetMapping("/equipment/musicMode")
    public SingleResponse<Integer> queryEquipmentMusicMode() {
        Long userId= BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        return SingleResponse.of(studentEquipmentService.queryEquipmentMusicMode(studentResp.getStudentId()));
    }

}
